<?php
namespace Home\Controller;
use Think\Controller;
class ProdectController extends Controller {
    public function detail($id){
    	$prodect=M('prodect')->where('id='.$id)->find();
    	$prodect_img=M('prodect_img')->where('prodect_id='.$id)->limit(5)->select();
    	$this->assign('prodect',$prodect);
    	$this->assign('prodect_img',$prodect_img);
    	$this->display();
	}
	public function lists($catalog_id){
		$brand_id=I('get.brand');
		$current_catalog=M('prodect_catalog')->where('id='.$catalog_id)->find();
		$parent_id=$current_catalog['parent_id'];
		$parent_catalog=M('prodect_catalog')->where('id='.$parent_id)->find();
		$brother_catalog=M('prodect_catalog')->where('parent_id='.$parent_id)->select();
		$child_catalog=M('prodect_catalog')->where('parent_id='.$catalog_id)->select();
		if($parent_id==0){
			$this->redirect('Home/Prodect/lists/catalog_id/'.$child_catalog[0]['id']);
		};
		$catalog_id=array($catalog_id);
		foreach ($child_catalog as $c) {
			$c=$c['id'];
			$catalog_id[]=$c;
		};
		$where='catalog_id in('.implode(',', $catalog_id).')';
		if($brand_id){
			$where=$where.'and brand_id='.$brand_id; 
		}
		$prodect=M('prodect')->where($where)->select();
		$brand_ids=M('prodect')->where('catalog_id in('.implode(',', $catalog_id).')')->distinct(true)->field('brand_id')->select();
		
		$brand_id=array();
		foreach ($brand_ids as $b) {
			$brand_id[]=$b['brand_id'];
		};
		//$brand_id=array_unique($brand_id);
		$brand=M('prodect_brand')->where('id in('.implode(',', $brand_id).')')->select();
		$this->assign('parent_catalog',$parent_catalog);
		$this->assign('brother_catalog',$brother_catalog);
		$this->assign('current_catalog',$current_catalog);
		$this->assign('prodect',$prodect);
		$this->assign('brand',$brand);

		$this->display();
	}
	
}